﻿using AutoMapper;
using Customer.API.Applcation.Command.ProcurementCommand;
using Customer.Domain;
using Customer.Infrstructre;
using Customer.Infrstructre.ProcurementManager;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Runtime.CompilerServices;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace Customer.API.Applcation.CommandHandlers.ProcurementCommandHandler
{
    public class ProcurementUpdCommandHandler : IRequestHandler<ProcurementUpdCommand, int>
    {

        IProcureProductRepository pRep;
        IProcurementRepository mRep;
        EFCoreDBcontext dbContext;

        public ProcurementUpdCommandHandler(IProcureProductRepository pRep, IProcurementRepository mRep, EFCoreDBcontext dbContext)
        {
            this.pRep = pRep;
            this.mRep = mRep;
            this.dbContext = dbContext;
        }

        public async Task<int> Handle(ProcurementUpdCommand request, CancellationToken cancellationToken)
        {
            using (var tran = await dbContext.Database.BeginTransactionAsync((System.Data.IsolationLevel.Serializable)))
            {

                try
                {
                    var se = (await mRep.GetAsync(x => x.ProcurementId == request.AddDto.ProcurementId)).FirstOrDefault();

                    se.ProcurementId = request.AddDto.ProcurementId;
                    se.ProcurementCode = request.AddDto.ProcurementCode;
                    se.ClientId = request.AddDto.ClientId;
                    se.DocumentType = request.AddDto.DocumentType;
                    se.AssociateNumber = request.AddDto.AssociateNumber;
                    se.VendorId = request.AddDto.VendorId;
                    se.Pcontact = request.AddDto.Pcontact;
                    se.Phone = request.AddDto.Phone;
                    se.ReceiptDate = request.AddDto.ReceiptDate;
                    se.PRremark = request.AddDto.PRremark;
                    se.MakePerson = request.AddDto.MakePerson;
                    se.MarkTime = request.AddDto.MarkTime;
                    se.Office_Id = request.AddDto.Office_Id;
                    se.User_Id = request.AddDto.User_Id;

                    // 先处理 procurement1
                    var taskToAdd = await mRep.Update(se);

                    foreach (var item in request.AddDto.ProductDetail)
                    {
                        var list = (await pRep.GetAsync(c => c.ProcurementCode == se.ProcurementCode)).FirstOrDefault();
                        list.ProcurementCode = se.ProcurementCode;
                        list.ProductsId = item.ProductsId;
                        list.ProcureNum = item.ProcureNum;
                        list.Office_Id = request.AddDto.Office_Id;
                        list.User_Id = request.AddDto.User_Id;
                        await pRep.Update(list);
                    }

                    // 提交事务
                    await tran.CommitAsync();

                    return 1;  // 返回最终的结果
                }
                catch (Exception)
                {
                    // 回滚事务
                    await tran.RollbackAsync();
                    throw;
                }
            }
        }
    }
}
